﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Globalization;

namespace WindowsFormsApplication1
{
    public partial class FormRegister : Form
    {
        ForumService forumService;
        bool invalid;

        public FormRegister(ForumService fs)
        {
            InitializeComponent();
            forumService = fs;
            this.panel1.Hide();
        }

        private void button1_Click(object sender, EventArgs e)//register
        {
            if (!IsValidEmail(maskedTextBox5.Text))
                new FormError("Invalid Email address!").Show();
            else
               
            {
                if (String.IsNullOrEmpty(maskedTextBox1.Text) || String.IsNullOrEmpty(maskedTextBox2.Text) || String.IsNullOrEmpty(maskedTextBox3.Text) || String.IsNullOrEmpty(maskedTextBox4.Text) || String.IsNullOrEmpty(maskedTextBox5.Text))
                    new FormError("One of the fields is empty!").Show();
                else
                    if (!checkInputs(maskedTextBox1.Text, maskedTextBox2.Text, maskedTextBox3.Text, maskedTextBox4.Text, maskedTextBox5.Text))
                        new FormError("Name is limited to 15 \r\nLast name is limited to 15 \r\nUser name is limited to 15 \r\nPassword is limited to 32 \r\nEmail is limited to 50 \r\n").Show();
                    else
                        {
                            forumService.register(maskedTextBox1.Text, maskedTextBox2.Text, maskedTextBox3.Text, maskedTextBox4.Text, maskedTextBox5.Text, this);
                            this.button1.Hide();
                            this.panel1.Show();
                        }
            }
        }


        private void button3_Click(object sender, EventArgs e)//confirm code
        {
            if (String.IsNullOrEmpty(maskedTextBox6.Text))
                new FormError("One of the fields is empty!").Show();
            else
            {
                string text = maskedTextBox6.Text.Replace(" ", "");
                this.forumService.confirmCode(maskedTextBox1.Text, maskedTextBox2.Text, maskedTextBox3.Text, maskedTextBox4.Text, maskedTextBox5.Text, text);
            }
        }

        private void FormRegister_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }



        public bool IsValidEmail(string strIn)
        {
            invalid = false;
            if (String.IsNullOrEmpty(strIn))
                return false;

            // Use IdnMapping class to convert Unicode domain names. 
            try
            {
                strIn = Regex.Replace(strIn, @"(@)(.+)$", this.DomainMapper,
                                      RegexOptions.None);
            }
            catch (Exception e)
            {
                return false;
            }

            if (invalid)
                return false;

            // Return true if strIn is in valid e-mail format. 
            try
            {
                return Regex.IsMatch(strIn,
                      @"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
                      @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$",
                      RegexOptions.IgnoreCase);
            }
            catch (Exception e)
            {
                return false;
            }
        }



        private string DomainMapper(Match match)
        {
            // IdnMapping class with default property values.
            IdnMapping idn = new IdnMapping();

            string domainName = match.Groups[2].Value;
            try
            {
                domainName = idn.GetAscii(domainName);
            }
            catch (ArgumentException)
            {
                invalid = true;
            }
            return match.Groups[1].Value + domainName;
        }


        private bool checkInputs(string name, string last, string user, string pass, string mail)
        {
            return (name.Length < 16 && last.Length < 16 && user.Length < 16 && pass.Length < 33 && mail.Length < 51);
        }


    }
}
